package com.news.news.service;

import com.news.news.dao.AdminDao;
import com.news.news.dao.IndexDao;
import com.news.news.modle.Link;
import com.news.news.modle.News;
import com.news.news.modle.NewsType;
import com.news.news.modle.System;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;
import java.util.concurrent.TimeUnit;


@Service
@Transactional
public class IndexService {
    @Resource
    IndexDao indexDao;

    @Resource
    RedisTemplate redisTemplate;

    public  List<NewsType> newsTypeList() {

        ValueOperations valueOperations = redisTemplate.opsForValue();
        List<NewsType> list= (List<NewsType>) valueOperations.get("newsType");
        if(list==null){
            list=indexDao.newsTypeList();
          valueOperations.set("newsType",list,10, TimeUnit.MINUTES);
        }
        return list;
    }

    public List<Link> getLink() {

        ValueOperations valueOperations = redisTemplate.opsForValue();
        List<Link> list= (List<Link>) valueOperations.get("Link");
        if(list==null){
            list=indexDao.getLink();
            valueOperations.set("Link",list,10, TimeUnit.MINUTES);
        }
        return list;
    }

    public System getSystem() {
        return indexDao.getSystem();
    }

    public List<News> getNews() {
//        ValueOperations valueOperations = redisTemplate.opsForValue();
//        List<News> list= (List<News>) valueOperations.get("News");
//        if(list==null){
        List<News>   list=indexDao.getNews();
//            valueOperations.set("News",list,10, TimeUnit.MINUTES);
//        }
        return list;

    }

    public List<News> findNewsByid(Integer id) {
        return indexDao.findNewsByid(id);
    }

    public List<News> queryNews(String name) {
        return indexDao.queryNews(name);
    }
}
